#include "topwidget.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPixmap>
#include "globalobject.h"

topwidget::topwidget(QWidget *parent) : QWidget(parent)
{
    m_ismax = false;
    setAttribute(Qt::WA_TranslucentBackground,true);
    setMouseTracking(true);
    setMinimumHeight(91);
    setMaximumHeight(91);
    initControl();
    setLayoutWidget();
}

topwidget::~topwidget()
{

}

void topwidget::initControl()
{
    m_minbtn = new state3tbutton(":/topwid/resources/skin/topwid/top_zxh.png",this);
    m_closebtn = new state3tbutton(":/topwid/resources/skin/topwid/top_close.png",this);
    m_minbtn->setFixedSize(30,30);
    m_closebtn->setFixedSize(30,30);
    connect(m_minbtn,SIGNAL(clicked()),gmainwid,SLOT(showMinimized()));
    connect(m_closebtn,SIGNAL(clicked()),gmainwid,SLOT(close()));

    //切换按钮
    m_taskinfobtn = new state3tbutton(":/topwid/resources/skin/topwid/custom_btn.png",this);
    m_taskresultbtn = new state3tbutton(":/topwid/resources/skin/topwid/freeze_btn.png",this);
    m_dragbtn = new state3tbutton(":/topwid/resources/skin/topwid/drag_btn.png",this);
    m_multilinebtn = new state3tbutton(":/topwid/resources/skin/topwid/multiline_btn.png",this);
    m_taskinfobtn->setGeometry(QRect(15,30,87,58));
    m_taskresultbtn->setGeometry(QRect(102,30,87,58));
    m_dragbtn->setGeometry(QRect(189,30,87,58));
    m_multilinebtn->setGeometry(QRect(276,30,87,58));
    connect(m_taskinfobtn,SIGNAL(clicked()),this,SLOT(setShowInfoWidget()));
    connect(m_taskresultbtn,SIGNAL(clicked()),this,SLOT(setShowResultWidget()));
    connect(m_dragbtn,SIGNAL(clicked()),this,SLOT(setShowDragWidget()));
    connect(m_multilinebtn,SIGNAL(clicked()),this,SLOT(setShowMultilineWidget()));
    setShowInfoWidget();
}

void topwidget::setLayoutWidget()
{
    QVBoxLayout *mianlayout = new QVBoxLayout(this);
    QHBoxLayout *toplayout = new QHBoxLayout;
    toplayout->addStretch();
    toplayout->addWidget(m_minbtn);
    toplayout->addWidget(m_closebtn);
    toplayout->setContentsMargins(6,0,0,0);
    toplayout->setSpacing(2);
    toplayout->setAlignment(Qt::AlignTop);
    mianlayout->addLayout(toplayout);
    mianlayout->setContentsMargins(0,0,0,0);
    setLayout(mianlayout);
}

void topwidget::setShowInfoWidget()
{
    m_taskinfobtn->isSelect(true);
    m_taskresultbtn->isSelect(false);
    m_dragbtn->isSelect(false);
    m_multilinebtn->isSelect(false);
    emit setCurrentIndex(0);
}

void topwidget::setShowResultWidget()
{
    m_taskinfobtn->isSelect(false);
    m_taskresultbtn->isSelect(true);
    m_dragbtn->isSelect(false);
    m_multilinebtn->isSelect(false);
    emit setCurrentIndex(1);
}

void topwidget::setShowDragWidget()
{
    m_taskinfobtn->isSelect(false);
    m_taskresultbtn->isSelect(false);
    m_dragbtn->isSelect(true);
    m_multilinebtn->isSelect(false);
    emit setCurrentIndex(2);
}

void topwidget::setShowMultilineWidget()
{
    m_taskinfobtn->isSelect(false);
    m_taskresultbtn->isSelect(false);
    m_dragbtn->isSelect(false);
    m_multilinebtn->isSelect(true);
    emit setCurrentIndex(3);
}

void topwidget::initBackGroundColor()
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

void topwidget::paintEvent(QPaintEvent *e)
{
    initBackGroundColor();
    QPainter painter(this);
    QPixmap topbackgroundpix(":/topwid/resources/skin/topwid/top_bg.png");
    painter.drawPixmap(0,0,rect().width(),91,topbackgroundpix);
    QWidget::paintEvent(e);
}

